<?php
class ctrl_api extends ctrl_base
{
    /**
     * @info　给微信用户发送文本消息
     * @author Jackie Yang
     * @date 20140521
     */
    public static function send_text(){
    	if(OPEN_WHITE_IP) {
    		if(!model_weixin::white_ip(WEIXIN_PUSH_IP)) {
    			$arr = array(
    					'errCode'	=> '99',
    					'message'	=> '非法请求，ip不在白名单'
    			);
    			framework_static_function::output_json($arr);
    		}
    	}
         
        $msg = isset($_POST['message'])? trim($_POST['message']):'';
        if($msg != '') {
        	$arrMsg = json_decode($msg,true);
        	$touser = trim($arrMsg['touser']);
        	$content = trim($arrMsg['content']);
        	if(empty($touser)) {
        		$arr = array(
        				'errCode'	=> '01',
        				'message'	=> '消息接收用户openid不为空'
        		);
        		framework_static_function::output_json($arr);
        	}
        	if(empty($content)) {
        		$arr = array(
        				'errCode'	=> '02',
        				'message'	=> '消息不为空'
        		);
        		framework_static_function::output_json($arr);
        	}
        	$result = model_send::send_text($touser,$touser);
        	if($result) {
        		$arr = array(
        				'errCode'	=> '00',
        				'message'	=> '消息发送成功'
        		);
        		framework_static_function::output_json($arr);
        	}
        }
    }
    
    /**
     * 微信用户绑定user_id
     */
    public static function save_userId() {
    	if(OPEN_WHITE_IP) {
    		if(!model_weixin::white_ip(WEIXIN_PUSH_IP)) {
    			$arr = array(
    					'errCode'	=> '99',
    					'message'	=> '非法请求，ip不在白名单'
    			);
    			framework_static_function::output_json($arr);
    		}
    	}
    	$msg = isset($_POST['message'])? trim($_POST['message']):'';
    	if($msg != '') {
    		$arrMsg = json_decode($msg,true);
    		if(isset($arrMsg['openId'])&&isset($arrMsg['userId'])) {
    			$data['user_id'] = $arrMsg['userId'];
    			$rowCount = model_user::update_weixin_user_info($data, $arrMsg['openId'], WEIXINHAO);
    			if($rowCount > 0) {
    				$arr = array(
    						'errCode'	=> '00',
    						'message'	=> '微信用户绑定user_id成功!'
    				);
    				framework_static_function::output_json($arr);
    			}
    		} else {
    			$arr = array(
    					'errCode'	=> '01',
    					'message'	=> '参数不对！'
    			);
    			framework_static_function::output_json($arr);
    		}
    	} else {
    		$arr = array(
    				'errCode'	=> '01',
    				'message'	=> '参数不对！'
    		);
    		framework_static_function::output_json($arr);
    	}
    }
}